package com.tiancheng.trade.settlement.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.exception.error.ArgumentErrorInfoEnum;
import com.tiancheng.trade.settlement.model.settlement.MerchantContract;
import com.tiancheng.trade.settlement.service.settlement.IMerchantContractService;
import com.tiancheng.trade.settlement.util.Res;
import com.tiancheng.trade.settlement.vo.settlement.merchantcontract.GetMerchantContractVO;
import com.tiancheng.trade.settlement.vo.settlement.merchantcontract.GetPageMerchantContractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/merchant/contract")
public class MerchantContractAdminController {

    @Autowired
    IMerchantContractService merchantService;

    /**
     * 按id查询商户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) throws BusinessException {
        Assert.isTrue(id != null, ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        return Result.success(merchantService.findById(id));
    }


    /**
     * 查询商户列表信息
     *
     * @param merchantVO
     * @return
     */
    @GetMapping("/findPageList")
    public Result<?> findMerchantPage(GetPageMerchantContractVO merchantVO) throws BusinessException {
        PageInfo<MerchantContract> result = merchantService.getMerchantPage(merchantVO);
        return Result.success(result);
    }

    /**
     * 查询商户list信息
     *
     * @param merchantVO
     * @return
     */
    @GetMapping("/findList")
    public Result findList(GetMerchantContractVO merchantVO) throws BusinessException {
        List<MerchantContract> result = merchantService.findList(merchantVO);
        return Result.success(result);
    }

    /**
     * 新增商户信息
     *
     * @param merchant
     * @return
     */
    @PostMapping("/")
    public Result add(@RequestBody MerchantContract merchant) throws BusinessException {
//        return merchantService.add(merchant);
        return Res.judge(merchantService.add(merchant), "新增");
    }

    /**
     * 修改商户信息
     *
     * @param merchant
     * @return
     */
    @PutMapping("/")
    public Result update(@RequestBody MerchantContract merchant) throws BusinessException {
        Assert.isTrue(merchant.getId() != null, ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        return Res.judge(merchantService.update(merchant), "修改");
    }

    /**
     * 按商户编号删除商户信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Long id) throws BusinessException {
        Assert.isTrue(id != null, ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        return Res.judge(merchantService.remove(id), "删除");
    }

    /**
     * 商户信息导出
     *
     * @param merchantVO
     * @return
     */
    @PostMapping("/export")
    public Result merchantContractExport(@RequestBody GetPageMerchantContractVO merchantVO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        merchantService.export(merchantVO, request, response);
        return Result.success("导出成功");
    }
}
